<?php
namespace app\manager\controller;

use app\common\lib\AdminController;
use app\manager\model\PermissionFileCache;
use app\manager\model\RoleModel;
use app\manager\model\RolePermissionModel;
use app\manager\model\validate\RoleValidate;
use think\Controller;
use think\Request;

class RolePermissionController extends AdminController
{
    /**
     * 普通权限分配
     *
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data = $_REQUEST;
        if (!isset($data['role_id'])) {
            return json(['code' => 0, 'msg' => 'param or role_id no exist']);
        }
        $model = new RolePermissionModel();
        if (request()->isPost()) {
            $permission_data = $this->format($data);
            $res = $model->dispatch($data['role_id'], $permission_data, RolePermissionModel::TYPE_PERMISSION);
            if (!$res) {
                return json(['code' => 0, 'msg' => $model->error_msg?:$model->getError()]);
            }
            // 清空权限文件缓存
            $fileCacheM = new PermissionFileCache();
            $fileCacheM->rmPermission();

            return json(['code' => 1, 'msg' => 'success']);
        }

        $permission_list = $model->permissionList($data['role_id']);
        $roleM = new RoleModel();
        $role = $roleM->field('name')->where(['id' => $data['role_id']])->find();
        $role_name = $role->getData('name');
        //var_dump($permission_list);die;
        $this->assign('permission_list', $permission_list);
        $this->assign('role_id', $data['role_id']);
        $this->assign('role_name', $role_name);
        return $this->fetch();
    }


    /**
     * 菜单权限的分配
     *
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function menu_index()
    {
        $data = $_REQUEST;
        if (!isset($data['role_id'])) {
            return json(['code' => 0, 'msg' => 'param type or role_id no exist']);
        }

        $model = new RolePermissionModel();
        if (request()->isPost()) {
            $permission_data = $this->format($data);
            $res = $model->dispatch($data['role_id'], $permission_data, RolePermissionModel::TYPE_MENU);
            if (!$res) {
                return json(['code' => 0, 'msg' => $model->error_msg?:$model->getError()]);
            }
            return json(['code' => 1, 'msg' => 'success']);
        }
        $permission_list = $model->menuPermissionList($data['role_id']);
        //var_dump($permission_list);die;
        $roleM = new RoleModel();
        $role = $roleM->field('name')->where(['id' => $data['role_id']])->find();
        $role_name = $role->getData('name');
        $this->assign('menu_list', $permission_list);
        $this->assign('role_id', $data['role_id']);
        $this->assign('role_name', $role_name);
        return $this->fetch();
    }

    /**
     * 格式化权限数据为单一的权限id数组
     *
     * @param $data
     * @return array
     */
    private function format($data) {
        $list = [];
        unset($data['role_id']);
        foreach ($data as $item) {
            if (is_string($item)) {
                $list[] = $item;
            } else if (is_array($item)) {
                $item = array_values($item);
                $list = array_merge($list, $item);
            }
        }
        return $list;
    }
}
